<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once("Core.php");
class CI_Mobile extends CI_Core{
	public $config = '../uploads/file/setting.php';
	
	function __construct(){
		parent::__construct();
		$this->database('default');
	}

	//保存APP菜单
	public function savemenu($site_id){
		$arr = $this->_read($this->config);
		$arr['appMenu'] = $this->ci->input->post('data');
		$this->_write($this->config,$arr);
	}
	//保存APP启动画面
	public function savesplash(){
		$arr = $this->_read($this->config);
		$arr['splash'] = $this->ci->input->post('splash');
		$this->_write($this->config,$arr);
	}
	public function getSplash(){
		$arr = $this->_read($this->config);
		return $this->status(true,$arr['splash']);
	}
	//保存引导屏
	public function saveGuide(){
		$arr = $this->_read($this->config);
		$arr['guides'] = $this->ci->input->post('data');
		$this->_write($this->config,$arr);
	}
	public function getGuide(){
		$arr = $this->_read($this->config);
		return $this->status(true,$arr['guides']);
	}
	//保存首页轮播
	public function saveSlides(){
		$arr = $this->_read($this->config);
		$arr['slides'] = $this->ci->input->post('data');
		$this->_write($this->config,$arr);
	}
	public function getSlides($v='mobile'){
		$f = $v=='mobile'?$this->config:'uploads/file/setting.php';
		$arr = $this->_read($f);
		return $this->status(true,$arr['slides']);
	}
	private function _read($f){
		if(!file_exists($f)) file_put_contents($f,"<?php\r\n");
		include($f);
		return $ENV?$ENV:array();
	}
	private function _write($f,$arr){
		foreach($arr as $k=>$v) $a[]='$ENV["'.$k.'"] = "'.str_replace('"','\"',$v).'";';
		file_put_contents($f, "<?php\r\n".implode("\r\n",$a)); 
	}
    //保存苹果设备TOKEN
    public function macToken($newToken, $oldToken){
        $rs = $this->get_one('*',array('token'=>$oldToken),'mac_devices');
        if($rs==array()){
            $this->db->insert('mac_devices',array('token'=>$newToken));
        }else{
            $this->db->update('mac_devices',array('token'=>$newToken),array('id'=>$rs['id']));
        }
    }
    public function userToken($username, $token){
        if(!$token || !$username) return $this->status(false, "Error parameters!");
        $this->db->update('user', array('mac_token'=>$token),array('username'=>$username));
        return $this->status(true, 'Success to bind device token!');
    }
}